(function() {
  "use strict";

  rapid.lodop = function() {
    /// <summary>
    /// 
    /// </summary>

    if(window.__LODOP__) {
      return window.__LODOP__;
    }

    window.__LODOP__ = getLodop(document.getElementById('LODOP'), document.getElementById('LODOP_EM'));

    if(rapid.isEmpty(window.__LODOP__)) {
      rapid._error_("rapid.lodop方法需要LODOP插件支持。");
    }

    window.__LODOP__.init = function() {
      this.___job_code___ = null;
      this.___catch_state___ = false;
      this.PRINT_INIT(arguments.length >= 1 ? arguments[0] : window.top.document.title);
      this.SET_PRINT_PAGESIZE(1, 0, 0, "");

      if(arguments.length === 2) {
        if(arguments[1] === true) {
          this.___catch_state___ = true;
          this.SET_PRINT_MODE("CATCH_PRINT_STATUS", true);
        }
      }
      return this;
    };

    window.__LODOP__.pagetype = function(type) {
      this.SET_PRINT_PAGESIZE(1, 0, 0, type);
      return this;
    };

    window.__LODOP__.add_print = function(content) {
      if(rapid.str.hasValue(content)) {
        var _func = "ADD_PRINT_TABLE";

        if(!content.r_startWith("<table")) {
          _func = "ADD_PRINT_HTM";
        }

        this[_func](0, 0, "100%", "100%", content);
        this.SET_PRINT_STYLEA(0, "LinkedItem", -1);
      }

      return this;
    };

    window.__LODOP__.print = function() {
      this.___job_code___ = this.PRINT();
      return this.___job_code___;
    };

    window.__LODOP__.preview = function() {
      this.___job_code___ = this.PREVIEW();
      return this.___job_code___;
    };

    window.__LODOP__.print_setup = function() {
      this.___job_code___ = this.PRINT_SETUP();
      return this.___job_code___;
    };

    window.__LODOP__.print_design = function() {
      this.___job_code___ = this.PRINT_DESIGN();
      return this.___job_code___;
    };

    window.__LODOP__.is_ok = function() {
      if(this.___catch_state___ === true) {
        return this.GET_VALUE("PRINT_STATUS_OK", this.___job_code___) !== "";
      }

      rapid.error("初始化时，没有设定第二个参数来决定是否要捕获打印机状态。");
    };

    window.__LODOP__.is_exit = function() {
      if(this.___catch_state___ === true) {
        return this.GET_VALUE("PRINT_STATUS_EXIST", this.___job_code___) !== "";
      }

      rapid.error("初始化时，没有设定第二个参数来决定是否要捕获打印机状态。");
    };

    window.__LODOP__.state = function(stateCode) {
      if(this.___catch_state___ === true) {
        return this.GET_VALUE("PRINT_STATUS_" + stateCode, this.___job_code___) === 0;
      }

      rapid.error("初始化时，没有设定第二个参数来决定是否要捕获打印机状态。");
    };

    return window.__LODOP__;
  };

})();